package cn.liweixi._05_transaction;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class producer {
    public static void main(String[] args) throws Exception {
        //创建一个生产者对象
        TransactionMQProducer producer = new TransactionMQProducer("hahaha-transaction");
        //连接nameserver
        producer.setNamesrvAddr("127.0.0.1:9876");
        //设置线程池
        ExecutorService excutorService = Executors.newFixedThreadPool(2);
        producer.setExecutorService(excutorService);
        //指定线程的事务监听
        producer.setTransactionListener(new MyTransactionCheckListener());
        //开启
        producer.start();
        //发送消息
        Message msg = new Message("topic-transaction", "tag-transaction", "菲菲菲菲非常美丽的巨兔".getBytes(RemotingHelper.DEFAULT_CHARSET));
        TransactionSendResult result = producer.sendMessageInTransaction(msg, null);
        System.out.println(result);
        //关闭
        producer.shutdown();

    }
}
